# -*- coding: utf-8 -*-
#   带checkBox选，第0列是选择列

# @Time    ： 2023/1/31  16:15
# @Auther  ： 徐良
# File     :  checkBoxNeDelegates.py


from PySide6.QtWidgets import QApplication, QStyleOptionButton, QStyle
from baseClass.baseDelegate import BaseDelegate
from PySide6.QtCore import Qt, Signal, QModelIndex
from mesParts.lineEdit.lineEdits import BodyLineEdit1
from mesParts.editPanel.editPanel import EditPanel      # 弹出字符编辑界面
from mesParts.dateTime.custDateTimes import CustDateEdit


class CheckBoxDelegate(BaseDelegate):
    """
    其它各列都不能编辑
    无选择条件
    option 参数是单选或多选，0是单选，1是多选
    """
    checkBoxSignal = Signal(QModelIndex)

    def __init__(self, args):
        """

        :param args:
        """
        super(CheckBoxDelegate, self).__init__()

        if 'selOption' in args:
            self.selOption = args['selOption']              # checkBox是单选还是多选
        else:
            self.selOption = None

        if 'widgetList' in args:                            # 网格中的控件，check为自画控制
            self.widgetList = args['widgetList']
        else:
            self.widgetList = []


    def paint(self, painter, option, index):
        col = index.column()
        if self.widgetList[col] == 'check':  # check 方框
            check_style = QStyleOptionButton()
            check_style.rect = option.rect
            check_style.rect.setX(int(option.rect.x() + option.rect.width() / 2 - 6))
            if index.data() == 1:
                check_style.state = QStyle.State_Enabled | QStyle.State_On
            else:
                check_style.state = QStyle.State_Enabled | QStyle.State_Off
            QApplication.style().drawControl(QStyle.CE_CheckBox, check_style, painter)

        else:
            super(CheckBoxDelegate, self).paint(painter, option, index)

    def createEditor(self, parent, option, index):
        col = index.column()
        # if col > 0 and self.isEditList[col]:
        #     editor = LineEdit1(parent)
        #     return editor

    def editorEvent(self, e, model, option, index):
        col = index.column()
        if self.widgetList[col] == 'check':
            if e.button() == Qt.LeftButton:
                if index.data():
                    model.setData(index, 0)
                    self.checkBoxSignal.emit(index)
                else:
                    if self.selOption == 0:
                        if col == 0:
                            pass
                            # self.clearModelSel(model)
                        model.setData(index, 1)
                        self.checkBoxSignal.emit(index)
                    else:
                        model.setData(index, 1)
                        self.checkBoxSignal.emit(index)
            return True
        return super(CheckBoxDelegate, self).editorEvent(e, model, option, index)

    def clearModelSel(self, model):
        """

        :return:
        """
        for i in range(0, model.rowCount()):
            index = model.index(i, 0)
            model.setData(index, 0)

#  ***************************************************


class CheckBoxEditDelegate(CheckBoxDelegate):
    """
    无色，无选择条件， 单选和多选， 可编辑
    option 参数是单选或多选，0是单选，1是多选
    """
    def __init__(self, args):
        super(CheckBoxEditDelegate, self).__init__(args)

        if 'isEditList' in args:
            self.isEditList = args['isEditList']              # 是否可编辑
        else:
            self.isEditList = []



    def createEditor(self, parent, option, index):

        col = index.column()
        if col >= 0 and self.isEditList[col]:
            if self.widgetList[col] == 'BodyLineEdit1':
                editor = BodyLineEdit1(parent)
                return editor
            elif self.widgetList[col] == 'EditPanel':
                editor = BodyLineEdit1(parent)
                return editor

            elif self.widgetList[col] == 'CustDateEdit':
                editor = CustDateEdit(parent)  # 注意此处修正，应该创建  对象
                return editor

            elif self.widgetList[col] == 'None':
                return None

            elif self.widgetList[col] == 'check':
                return None
            else:
                return None


    def editorEvent(self, event, model, option, index):
        # 首先调用父项的实现
        result = super().editorEvent(event, model, option, index)

        col = index.column()
        if self.widgetList[col] == 'EditPanel':
            if event.button() == Qt.LeftButton:
                # 获取字段值
                value = model.data(index, Qt.DisplayRole)
                # 获取字段名
                hn = model.headerData(col, Qt.Horizontal, Qt.DisplayRole)  # 修复了此处的错误
                paraMap = {'value': value, 'index': index, 'model': model}

                self.editPanel = EditPanel(paraMap)
                self.editPanel.setWindowTitle('正在编辑【' + hn + '】字段')
                self.editPanel.editContent.connect(self.makeRet)
                self.editPanel.show()

        return True if result else False  # 确保在所有条件之后都返回 True 或 False


    def makeRet(self, retMap):
        model = retMap['model']
        index = retMap['index']
        value = retMap['value']
        model.setData(index, value, Qt.EditRole)

